import {Component, OnInit} from '@angular/core';
import {NzMessageModule, NzMessageService, NzMessageServiceModule} from 'ng-zorro-antd/message';
import {DatePipe} from '@angular/common';
import {HireDeviceService} from '../../hireDevice.service';

@Component({
  selector: 'app-hot-search',
  templateUrl: './hot-search.component.html',
  styleUrls: ['./hot-search.component.scss']
})
export class HotSearchComponent implements OnInit {
  query = {
    status: '',
    pageNum: 1,
    pageSize: 99999,
  };
  STATUS = [
    '未开始',
    '进行中',
    '已删除',
    '已结束',
  ];
  // 表格数据
  listOfData = {
    page: 1,
    total: 0,
    records: []
  };
  editModal = {
    visible: false,
    id: '',
    word: '',
    status: 1,
  };

  constructor(
    private nzMessageService: NzMessageService,
    private datePipe: DatePipe,
    public hireDeviceService: HireDeviceService
  ) {
  }

  ngOnInit(): void {
    this.getHotSearchList();
  }

  getHotSearchList() {
    this.listOfData.records = [];
    this.hireDeviceService.getHotSearchList(this.query).then((res: any) => {
      console.log(res);
      if (res.code === 0) {
        this.listOfData = res.data;
      }
    });
  }

  // 1启用 0禁用
  changeStatus(data: any, status: 1 | 0) {
    this.hireDeviceService.editHotSearch({
      id: data.id,
      word: data.word,
      status,
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getHotSearchList();
      }
    });
  }

  // 1上移 0下移
  move(id: any, moveType: 1 | 0) {
    this.hireDeviceService.moveHotSearch({id, moveType}).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getHotSearchList();
      }
    });
  }

  showEdit(data?: any) {
    if (data) {
      this.editModal.id = data.id;
      this.editModal.word = data.word;
      this.editModal.status = data.status;
    }
    this.editModal.visible = true;
  }

  hideEditModal() {
    this.editModal = {
      visible: false,
      id: '',
      word: '',
      status: 1,
    };
  }

  saveHotSearch() {
    if (!this.editModal.word) {
      this.nzMessageService.error('请输入名称');
      return;
    }
    // if (!this.editModal.date.length) {
    //   this.nzMessageService.error('请选择生效时间');
    //   return;
    // }
    this.hireDeviceService.editHotSearch({
      id: this.editModal.id,
      word: this.editModal.word,
      status: this.editModal.status,
      // hpicUrl: this.editModal.hpicUrl || '',
      // hurl: this.editModal.hurl || '',
      // beginTime: this.datePipe.transform(this.editModal.date[0], 'yyyy-MM-dd HH:mm:ss'),
      // endTime: this.datePipe.transform(this.editModal.date[1], 'yyyy-MM-dd HH:mm:ss'),
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideEditModal();
        this.getHotSearchList();
      }
    });
  }

  deleteHotSearch(id: any) {
    this.hireDeviceService.deleteHotSearch({id, status: 2}).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getHotSearchList();
      }
    });
  }
}
